﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPMVC_first_app.Models;
using System.Data.Entity;

namespace ASPMVC_first_app.Controllers
{
    public class MoviesController : Controller
    {
        private ApplicationDbContext _context;

        public MoviesController()
        {
            _context = new ApplicationDbContext();
        }

        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
        }

        public ActionResult Index()
        {
            List<Movie> movieList = _context.Movies.ToList();

            return View(movieList);
        }

        public ActionResult MovieInfo(int id)
        {

            var movie = _context.Movies.Include(c => c.Genre).SingleOrDefault(c => c.Id == id);

            if (movie == null)
                return HttpNotFound();

            return View(movie);
        }

        [HttpPost]
        public ActionResult Save(ViewModels.MovieFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                var viewModell = new ViewModels.MovieFormViewModel()
                {
                    Movie = viewModel.Movie,
                    Genres = _context.Genres.ToList()
                };
                return View("MovieForm", viewModell);
            }

            if (viewModel.Movie.Id == 0)
            {
                _context.Movies.Add(viewModel.Movie);
            }
            else
            {
                var movieInDb = _context.Movies.Single(c => c.Id == viewModel.Movie.Id);

                //Mapper.Map(customer, customerInDb);

                movieInDb.Name = viewModel.Movie.Name;
                movieInDb.Description = viewModel.Movie.Description;
                movieInDb.GenreId = viewModel.Movie.GenreId;
                movieInDb.Received = viewModel.Movie.Received;
                movieInDb.Count = viewModel.Movie.Count;
            }

            _context.SaveChanges();
            return RedirectToAction("Index", "Movies");
        }

        public ActionResult New()
        {
            var genres = _context.Genres.ToList();
            var viewModel = new ViewModels.MovieFormViewModel()
            {
                Movie = new Movie(),
                Genres = genres
            };
            ViewBag.Action = "Add New Movie";
            return View("MovieForm", viewModel);
        }

        public ActionResult Edit(int id)
        {
            var movie = _context.Movies.SingleOrDefault(c => c.Id == id);

            if (movie == null)
                return HttpNotFound();

            var viewModel = new ViewModels.MovieFormViewModel()
            {
                Movie = movie,
                Genres = _context.Genres.ToList()
            };
            ViewBag.Action = "Edit Movie";
            return View("MovieForm", viewModel);
        }
    }
}